package com.chianghao.rock.system.controller;

import java.util.List;
import java.util.stream.Collectors;

import javax.validation.constraints.NotBlank;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chianghao.core.mybatis.plugins.Paging;
import com.chianghao.core.web.R;
import com.chianghao.core.web.ValidatedGroup;
import com.chianghao.rock.admin.system.system_param.dto.SystemParamDto;
import com.chianghao.rock.admin.system.system_param.dto.SystemParamModifyDto;
import com.chianghao.rock.admin.system.system_param.vo.SystemParamVo;
import com.chianghao.rock.common.SequenceService;
import com.chianghao.rock.system.entity.SystemParam;
import com.chianghao.rock.system.service.system_param.SystemParamService;
import com.rock.microshop.common_api.dto.PageDto;
import com.rock.microshop.common_api.vo.PageVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping("system/system_param")
@Api(value="系统参数",tags={"系统参数"})
public class SystemParamController {

    @Autowired
    SystemParamService systemParamService;

    @Autowired
    SequenceService sequenceService;

	@PostMapping("add")
	@ApiOperation(value="添加")
	public R<Boolean> add(@Validated({ValidatedGroup.Add.class}) SystemParamDto dto) {
		SystemParam systemParam = toEntity(dto);
		systemParam.setDeletable(true);
		systemParamService.doAdd(systemParam);
		return R.getSuccess(true);
	}

    @PostMapping("modify")
    @ApiOperation(value="修改")
    public R<Boolean> modify(@Validated({ValidatedGroup.Modify.class}) SystemParamModifyDto dto) {
    	SystemParam systemParam = new SystemParam();
    	systemParam.setCode(dto.getNewCode());
    	systemParam.setName(dto.getName());
    	systemParam.setRemark(dto.getRemark());
    	systemParam.setValue(dto.getValue());
		systemParamService.update(systemParam,dto.getCode());
    	return R.getSuccess(true);
	}

    @DeleteMapping("delete")
    @ApiOperation(value="删除")
    @ApiImplicitParam(name = "code", value = "编号", paramType = "query", required = true, dataType = "String")
    public R<Boolean> delete(@NotBlank(message="error:code,not.null") String code) {
    	systemParamService.doDelete(code);
    	return R.getSuccess(true);
    }

    @GetMapping("detail")
    @ApiOperation(value="明细")
    @ApiImplicitParam(name = "code", value = "编号", paramType = "query", required = true, dataType = "String")
    public R<SystemParamVo> detail(@NotBlank(message="error:code,not.null") String code) {
    	SystemParam  bean = systemParamService.queryByCode(code);
    	return R.getSuccess(toVo(bean));
    }

//    @GetMapping("query")
//    @ApiOperation(value="明细")
//    public R<List<SystemParamVo>> query(SystemParamDto dto) {
//    	List<SystemParam> List = systemParamService.query(toEntity(dto));
//    	return R.getSuccess(toListVo(List));
//    }

    @GetMapping("page-query")
    @ApiOperation(value="分页查询")
    public R<PageVo<SystemParamVo>> queryPageList(SystemParamDto dto, PageDto page) {
    	Paging paging  = new Paging(page.getCurrent(),page.getPageSize()); 
    	List<SystemParam> List = systemParamService.queryPage(toEntity(dto),paging );
    	PageVo<SystemParamVo> pageVo = new PageVo<SystemParamVo>();
    	pageVo.setItems(toListVo(List));
    	pageVo.setTotal(paging.getTotalRows());
    	return R.getSuccess(pageVo);
    }
    
    
    private SystemParamVo toVo(SystemParam bean) {
    	SystemParamVo vo = new SystemParamVo();
    	BeanUtils.copyProperties(bean, vo);
    	return vo;
    }
    
    private List<SystemParamVo> toListVo(List<SystemParam> list){
    	List<SystemParamVo> array = list.stream().map(item->toVo(item)).collect(Collectors.toList());
    	return array;
    }

    private SystemParam toEntity(SystemParamDto dto) {
    	SystemParam bean = new SystemParam();
    	BeanUtils.copyProperties(dto, bean);
    	return bean;
    }

}